module app.repository.UserRepository;

import hunt.entity.repository;
import hunt.framework.Simplify;
import app.model.User;

class UserRepository : EntityRepository!(User, ulong)
{
    this()
    {
        super(defaultEntityManager());
    }

    /**
    * 通过用户名和密码查询
    * @Parameters    username    string
    * @Parameters    pwd    string
    * @return    User
    */
    User findByUsernameAndPwd(string username, string pwd)
    {
        import std.digest : toHexString;
        import std.digest.md : MD5Digest;
        import std.uni : toLower;

        // md5加密
        string encodePwd = toLower(toHexString(new MD5Digest().digest(pwd)));
        // import std.stdio;
        // writeln(encodePwd);
        return _manager.createQuery!(User)("SELECT * FROM user WHERE username = :username AND password = :pwd")
            .setParameter("username", username).setParameter("pwd", encodePwd).getSingleResult();
    }
}
